package com.sxyc.tintin.model;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;

public class DeviceBinding extends Model<DeviceBinding> {

	public static final DeviceBinding dao = new DeviceBinding();

	// 使用jFinal 日志
	private com.jfinal.log.Log log = com.jfinal.log.Log.getLog(this.getClass());

	// public DeviceBinding getDeviceBinding(long deviceId){
	// return DeviceBinding.dao.findFirst("select * from DeviceBinding where
	// deviceId = ? ", new Object[] { deviceId });
	// }

	public DeviceBinding getDeviceBinding(long deviceId, String openId) {
		return DeviceBinding.dao.findFirst("select * from DeviceBinding where deviceId = ? and openId = ? ",
				new Object[] { deviceId, openId });
	}

	public boolean addDeviceBinding(long deviceId, String openId) {
		boolean isSuccess = false;
		try {
			DeviceBinding binding = DeviceBinding.dao.getDeviceBinding(deviceId, openId);
			if (binding != null) {
				binding.set("BindingTime", new Date());
				binding.set("IsAlarm", 1);
				binding.set("Status", 1);
				binding.set("UpdatedBy", "System");
				binding.set("UpdatedDate", new Date());
				isSuccess = binding.update();
			} else {
				binding = new DeviceBinding();
				binding.set("AppId", "wxc0feefca7c7dc010");
				binding.set("DeviceId", deviceId);
				binding.set("OpenId", openId);
				binding.set("BindingTime", new Date());
				binding.set("IsAlarm", 1);
				binding.set("Status", 1);
				binding.set("CreatedBy", "System");
				binding.set("CreatedDate", new Date());
				binding.set("UpdatedBy", "System");
				binding.set("UpdatedDate", new Date());
				isSuccess = binding.save();
				if (isSuccess) {
					log.error("更新设备 [ " + deviceId + " ] 绑定微信 [ " + openId + " ] 关联关系成功！");
				} else {
					log.error("更新设备 [ " + deviceId + " ] 绑定微信 [ " + openId + " ] 关联关系失败！");
				}
			}
		} catch (Exception ex) {
			log.error("更新设备 [ " + deviceId + " ] 绑定微信 [ " + openId + " ] 关联关系异常！原因：" + ex.getMessage());
		}
		return isSuccess;
	}
	
	public DeviceBinding getWxUserByOpenId(String openId){
		return DeviceBinding.dao.findFirst("select * from devicebinding where OpenId = ? and status = 1 ", new Object[] { openId });
	}
	
	public DeviceBinding getWxUserByDeviceId(String deviceId){
		return DeviceBinding.dao.findFirst("select * from devicebinding where DeviceId = ? and status = 1 ", new Object[] { deviceId });
	}
	
	public boolean deleteDeviceBinding(long deviceId, String openId) {
		boolean isSuccess = false;
		try {
			DeviceBinding deviceBinding = getDeviceBinding(deviceId, openId);
			if(deviceBinding!=null){
				isSuccess = deviceBinding.delete();
			}else{
				isSuccess = true;
			}
			if (isSuccess) {
				log.info("删除设备 [ " + deviceId + " ] 绑定微信 [ " + openId + " ] 关联关系成功！");
			} else {
				log.error("删除设备 [ " + deviceId + " ] 绑定微信 [ " + openId + " ] 关联关系失败！");
			}
		} catch (Exception ex) {
			log.error("删除设备 [ " + deviceId + " ] 绑定微信 [ " + openId + " ] 关联关系异常！原因：" + ex.getMessage());
		}
		return isSuccess;
	}


	/**
	 * 用户——设备 一绑一   清除接口
	 * @param openId
	 * @return
	 */
	public void clearDeviceBinding(String openId) {
		DeviceBinding deviceBinding = getWxUserByOpenId( openId );
		if(deviceBinding!=null)
			deviceBinding.delete();
	}
	
	
}
